/*
 * Created on Jul 12, 2008
 */
package edu.swri.swiftvis.plot.p3d.styles;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.JComponent;
import javax.swing.JPanel;

import edu.swri.swiftvis.DataFormula;
import edu.swri.swiftvis.plot.Plot;
import edu.swri.swiftvis.plot.PlotArea3D;
import edu.swri.swiftvis.plot.p3d.DrawAttributes;
import edu.swri.swiftvis.plot.p3d.RenderEngine;
import edu.swri.swiftvis.plot.p3d.Sphere3D;
import edu.swri.swiftvis.plot.p3d.Style3D;
import edu.swri.swiftvis.plot.p3d.Vect3D;
import edu.swri.swiftvis.plot.util.ColorModel;
import edu.swri.swiftvis.util.EditableString;

public class ScatterStyle3D implements Style3D {

    public ScatterStyle3D(PlotArea3D pa) {
        plotArea=pa;
    }

    public ScatterStyle3D(ScatterStyle3D c,PlotArea3D pa) {
        plotArea=pa;
        name.setValue(c.name.getValue());
        xFormula.setFormula(c.xFormula.getFormula());
        yFormula.setFormula(c.yFormula.getFormula());
        zFormula.setFormula(c.zFormula.getFormula());
        sizeFormula.setFormula(c.sizeFormula.getFormula());
        opacityFormula.setFormula(c.opacityFormula.getFormula());
        reflectFormula.setFormula(c.reflectFormula.getFormula());
        colorModel=new ColorModel(c.colorModel);
    }

    public Style3D copy(PlotArea3D c) {
        return new ScatterStyle3D(this,c);
    }

    public JComponent getPropertiesPanel() {
        if(propPanel==null) {
            propPanel=new JPanel(new BorderLayout());
            JPanel northPanel=new JPanel(new GridLayout(12,1));
            northPanel.add(name.getLabeledTextField("Name",null));
            northPanel.add(xFormula.getLabeledTextField("X Formula",null));
            northPanel.add(yFormula.getLabeledTextField("Y Formula",null));
            northPanel.add(zFormula.getLabeledTextField("Z Formula",null));
            northPanel.add(sizeFormula.getLabeledTextField("Size Formula",null));
            colorModel.addGUIToPanel("Color",northPanel);
            northPanel.add(opacityFormula.getLabeledTextField("Opacity",null));
            northPanel.add(reflectFormula.getLabeledTextField("Reflectivity",null));
            propPanel.add(northPanel,BorderLayout.NORTH);
        }
        return propPanel;
    }

    public void renderToEngine(RenderEngine engine) {
        Plot sink=plotArea.getSink();
        int[] range=xFormula.getSafeElementRange(sink, 0);
        DataFormula.mergeSafeElementRanges(range,yFormula.getSafeElementRange(sink, 0));
        DataFormula.mergeSafeElementRanges(range,zFormula.getSafeElementRange(sink, 0));
        DataFormula.mergeSafeElementRanges(range,sizeFormula.getSafeElementRange(sink, 0));
        DataFormula.mergeSafeElementRanges(range,opacityFormula.getSafeElementRange(sink, 0));
        DataFormula.mergeSafeElementRanges(range,reflectFormula.getSafeElementRange(sink, 0));
        DataFormula.checkRangeSafety(range,sink);
        
        for(int i=range[0]; i<range[1]; ++i) {
            Vect3D center=new Vect3D(xFormula.valueOf(sink,0, i),yFormula.valueOf(sink,0, i),zFormula.valueOf(sink,0, i));
            double radius=sizeFormula.valueOf(sink,0, i)/2;
            Color color=colorModel.getColor(sink,i);
            DrawAttributes attrs=new DrawAttributes(color,reflectFormula.valueOf(sink,0, i),opacityFormula.valueOf(sink,0, i));
            engine.add(new Sphere3D(center,radius), attrs);
        }
    }
    
    public String toString() {
        return "Scatter 3D - "+name.getValue();
    }
    
    public static String getTypeDescription() {
        return "Scatter Plot 3D";
    }

    private PlotArea3D plotArea;
    private EditableString name=new EditableString("Default");
    
    private DataFormula xFormula=new DataFormula("v[0]");
    private DataFormula yFormula=new DataFormula("v[1]");
    private DataFormula zFormula=new DataFormula("v[2]");
    private DataFormula sizeFormula=new DataFormula("1");
    private ColorModel colorModel=new ColorModel();
    private DataFormula opacityFormula=new DataFormula("1");
    private DataFormula reflectFormula=new DataFormula("0");
    
    private transient JPanel propPanel;

    private static final long serialVersionUID = -4764717067033439513L;
}
